<html>
<head><meta charset="utf-8"><title>Feature-gated enum variants · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html">Feature-gated enum variants</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228452498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228452498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228452498">(Mar 02 2021 at 17:41)</a>:</h4>
<p>Is there any guidance regarding feature-gating public enum variants? They seem like a bad idea because enabling a feature can break unrelated code that matches on the enum without a <code>_</code> fallback. I know that marking the enum as non-exhaustive prevents that, but it still feels pretty non-idiomatic to me.</p>



<a name="228453599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228453599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228453599">(Mar 02 2021 at 17:46)</a>:</h4>
<p>Why would making the enum non-exhaustive be non-idiomatic? That sounds like exactly what you're asking for.</p>



<a name="228454070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228454070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228454070">(Mar 02 2021 at 17:49)</a>:</h4>
<p>Marking it as non-exhaustive is idiomatic, but it feels that it might be better to not feature-gate the variants in the first place. Though I guess it's pretty hard to give any other advice without more details.</p>



<a name="228461666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228461666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228461666">(Mar 02 2021 at 18:34)</a>:</h4>
<p>cc <a href="https://github.com/rust-lang/rust/issues/71481">#71481</a></p>



<a name="228630544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228630544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228630544">(Mar 03 2021 at 17:19)</a>:</h4>
<p>I feel like the alternative would be worse. If there are variants that can never occur with a given feature disabled, making the user handle those variants seems unfortunate, when they know they can never happen.</p>



<a name="228631606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228631606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228631606">(Mar 03 2021 at 17:25)</a>:</h4>
<p>The alternative would be dropping the feature and leaving it always enabled. But I can see how some users might not like that. <a href="https://github.com/georust/gdal/issues/169">https://github.com/georust/gdal/issues/169</a> is what prompted me to ask.</p>



<a name="228631675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228631675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228631675">(Mar 03 2021 at 17:25)</a>:</h4>
<p>As prior art, <code>postgres</code> feature-gates <code>chrono</code>, but doesn't have an enum with the supported types</p>



<a name="228632295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228632295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228632295">(Mar 03 2021 at 17:29)</a>:</h4>
<p>The enum is <a href="https://github.com/georust/gdal/blob/master/src/vector/feature.rs#L457-L473">https://github.com/georust/gdal/blob/master/src/vector/feature.rs#L457-L473</a>. In this case the set of types is more or less closed, and it's understandable why the crate went with an enum instead of conversion traits</p>



<a name="228633426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228633426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228633426">(Mar 03 2021 at 17:36)</a>:</h4>
<p>An option in the future, but which won't be compiler-checked unless we have some feature agnostic <code>cargo check</code> of some sorts, would be to have downstream users of your code match on:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">that_enum</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[cfg(accessible(::that_crate::ThatEnum::ThatVariant))]</span><span class="w"></span>
<span class="w">    </span><span class="n">ThatEnum</span>::<span class="n">ThatVariant</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="err">…</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="err">…</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="err">…</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>should they want to match exhaustively <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>
<hr>
<p>I guess a more idiomatic approach would be to have <code>that_crate</code> start by defining their enum as:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">ThatEnum</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">ThatVariant</span><span class="p">(</span><span class="n">ThatVariant</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="err">…</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[cfg(not(feature…))]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">ThatVariant</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">_void</span>: ::<span class="n">core</span>::<span class="n">convert</span>::<span class="n">Infallible</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="cp">#[cfg(feature…)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">ThatVariant</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="err">…</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="228692002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Feature-gated%20enum%20variants/near/228692002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Feature-gated.20enum.20variants.html#228692002">(Mar 04 2021 at 00:06)</a>:</h4>
<p>If it's non-exhaustive then it doesn't matter if they're feature gated or not, because the user needs a <code>_ =&gt; </code> arm no matter what.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>